﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using FineUI;
using SysManage.BLL;
using ZHDJ.Data.Models.BaseInfo;
using ZHDJ.Data.Models.Edu;

namespace SysManage.Module.BaseInfo
{
    public partial class DataDictionaryList : BLL.PageBase
    {
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "DataDictionaryView";
            }
        }

        #endregion

        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                firstLoad.Text = "1";
                LoadData();
            }

        }

        private void LoadData()
        {
            // 权限检查
            CheckPowerWithButton("DataDictionaryAdd", btnNew);
            CheckPowerWithButton("DataDictionaryDelete", btnDeleteSelected);

            BindGrid1();

            // 默认选中第一个
            Grid1.SelectedRowIndex = 0;

            string DDicType = GetSelectedDataKeyID(Grid1);
            //ResolveDeleteButtonForGrid(btnDeleteSelected, Grid2, "确定要从当前职称移除选中的{0}项记录吗？");
            btnNew.OnClientClick = Window1.GetShowReference("~/app/SysManage/Module/BaseInfo/DataDictionaryEdit.aspx?DDicType=" + DDicType, "新增");

            // 每页记录数
            Grid2.PageSize = ConfigHelper.PageSize;
            ddlGridPageSize.SelectedValue = ConfigHelper.PageSize.ToString();

            BindGrid2();
        }

        private void BindGrid1()
        {
           
            // 排列
            var ds = DataDictionary.GetDDicTypeList(0,int.MaxValue);
            Grid2.RecordCount = Convert.ToInt32(ds.Tables[1].Rows[0][0]);
            Grid1.DataSource = ds;
            Grid1.DataBind();
        }

        private void BindGrid2()
        {
            string selectId = GetSelectedDataKeyID(Grid1);

            if (selectId == "-1")
            {
                Grid2.RecordCount = 0;

                Grid2.DataSource = null;
                Grid2.DataBind();
            }
            else
            {
                IQueryable<DataDictionary> q = DB.DataDictionarys;

                // 搜索内容

                q = q.Where(u => u.DDicType == selectId);
                
              
                // 在查询添加之后，排序和分页之前获取总记录数
                Grid2.RecordCount = q.Count();

                // 排列和分页
                q = SortAndPage<DataDictionary>(q, Grid2);

                Grid2.DataSource = q;
                Grid2.DataBind();
            }

        }


        #endregion

        #region Events

        protected void ddlGridPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            Grid2.PageSize = Convert.ToInt32(ddlGridPageSize.SelectedValue);

            BindGrid2();
        }


        #endregion

        #region Grid1 Events

        protected void Grid1_Sort(object sender, GridSortEventArgs e)
        {
            Grid1.SortDirection = e.SortDirection;
            Grid1.SortField = e.SortField;
            BindGrid1();

            // 默认选中第一个职称
            Grid1.SelectedRowIndex = 0;

            BindGrid2();
        }

        protected void Grid1_RowClick(object sender, FineUI.GridRowClickEventArgs e)
        {
            string DDicType = GetSelectedDataKeyID(Grid1);
            btnNew.OnClientClick = Window1.GetShowReference("~/app/SysManage/Module/BaseInfo/DataDictionaryEdit.aspx?DDicType=" + DDicType, "新增");
            BindGrid2();
            if (Grid1.SelectedRowIndex != 0)
            {
                PageContext.RegisterStartupScript("removeSelectCss()");
            }
        }

        #endregion

        #region Grid2 Events

        protected void ttbSearchUser_Trigger2Click(object sender, EventArgs e)
        {
            //ttbSearch.ShowTrigger1 = true;
            BindGrid2();
        }

        protected void ttbSearchUser_Trigger1Click(object sender, EventArgs e)
        {
           // ttbSearch.Text = String.Empty;
           // ttbSearch.ShowTrigger1 = false;
            BindGrid2();
        }

        protected void Grid2_PreDataBound(object sender, EventArgs e)
        {
            // 数据绑定之前，进行权限检查
            CheckPowerWithLinkButtonField("DataDictionaryDelete", Grid2, "deleteField");
        }

        protected void Grid2_Sort(object sender, GridSortEventArgs e)
        {
            Grid2.SortDirection = e.SortDirection;
            Grid2.SortField = e.SortField;
            BindGrid2();
        }

        protected void Grid2_PageIndexChange(object sender, GridPageEventArgs e)
        {
            Grid2.PageIndex = e.NewPageIndex;
            BindGrid2();
        }

        protected void btnDeleteSelected_Click(object sender, EventArgs e)
        {
            // 在操作之前进行权限检查
            if (!CheckPower("DataDictionaryDelete"))
            {
                CheckPowerFailWithAlert();
                return;
            }

            List<string> ids = GetSelectedDataKeyIDs(Grid2);
            DB.DataDictionarys.Where(u => ids.Contains(u.ID)).ToList().ForEach(u => DB.DataDictionarys.Remove(u));
            DB.SaveChanges();

            // 清空当前选中的项
            Grid2.SelectedRowIndexArray = null;

            // 重新绑定表格
            BindGrid2();
        }


        protected void Grid2_RowCommand(object sender, GridCommandEventArgs e)
        {
            object[] values = Grid2.DataKeys[e.RowIndex];
            string ID = Convert.ToString(values[0]);

            if (e.CommandName == "Delete")
            {
                //var userCount = DB.PatyEduSubjects.Count(t => t.SpecialID == patyEduSpecialId);
                //if (userCount > 0)
                //{
                //    Alert.ShowInTop("删除失败！需要先清空作用此专题的题目！");
                //    return;
                //}

                var del = DB.DataDictionarys.FirstOrDefault(t => t.ID == ID);
                DB.DataDictionarys.Remove(del);
                DB.SaveChanges();
                BindGrid2();

            }
        }
   

        protected void Window1_Close(object sender, EventArgs e)
        {
            BindGrid2();
        }

      
        protected void rblEnableStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindGrid2();
        }
        #endregion

    }
}
